package com.ysm.smfu.activity;

import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ysm.smfu.R;
import com.ysm.smfu.adapter.DefaultAdapter;
import com.ysm.smfu.bean.VersionInfo;
import com.ysm.smfu.holder.BaseHolder;
import com.ysm.smfu.holder.HistoryVersionHolder;
import com.ysm.smfu.protocal.BaseProtocal;
import com.ysm.smfu.protocal.MyStringCallBack;
import com.ysm.smfu.utils.JsonUtil;
import com.ysm.smfu.utils.PrintUtil;
import com.ysm.smfu.view.RefreshLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HistoryVersionActivity extends BaseRefreshActivity<VersionInfo.HistoryVersionInfo> {

    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.refresh)
    RefreshLayout refreshLayout;
    @BindView(R.id.mainActivity_topbar_img)
    ImageView mainActivityTopbarImg;
    @BindView(R.id.title_body)
    TextView titleBody;
    private MyAdapter adapter;

    @Override
    protected void initViews() {
        super.initViews();
        setContentView(R.layout.activity_history_version);
        ButterKnife.bind(this);
        initRefresh(refreshLayout, true);
        titleBody.setText("历史版本");
        mainActivityTopbarImg.setVisibility(View.GONE);
    }

    @Override
    protected void initListener() {
        super.initListener();
        adapter = new MyAdapter(datas, listView, null);
        listView.setAdapter(adapter);
    }

    //  加载数据
    @Override
    public void load(boolean isLoad) {
        if (isLoad)
            page++;
        showHistoryVersion();
    }

    @Override
    protected void onResume() {
        super.onResume();
        showHistoryVersion();
    }

    /**
     * 显示历史版本
     */
    private void showHistoryVersion() {
        BaseProtocal.builder(false).getHistoryVersion(new MyStringCallBack() {
            @Override
            public void onSuccess(String response) {
                PrintUtil.printRespones("历史版本", response);
                String state = JsonUtil.getState(response);
                if (TextUtils.equals(state, "1")) {
                    VersionInfo versionInfo = new Gson().fromJson(response, VersionInfo.class);
                    List<VersionInfo.HistoryVersionInfo> historyVersionInfos = versionInfo.versionInfo;
                    setLoadData(historyVersionInfos);
                    adapter.notifyDataSetChanged();
                } else {
                    onRefreshError();
                }
            }

            @Override
            public void onFail(Exception e) {
                onRefreshError();
            }
        }, page);
    }

    class MyAdapter extends DefaultAdapter<VersionInfo.HistoryVersionInfo> {
        public MyAdapter(List<VersionInfo.HistoryVersionInfo> datas, ListView listView, Fragment fragment) {
            super(datas, listView, fragment);
        }

        @Override
        protected BaseHolder<VersionInfo.HistoryVersionInfo> getHolder() {
            return new HistoryVersionHolder();
        }
    }
}
